import { All, Controller, Get, Param, ParseIntPipe, Post, Req, Res, UseFilters } from '@nestjs/common'
import { Request } from 'express'
import { ApiService } from '@main/service/ApiService'
import { ApiTags } from '@nestjs/swagger'
import { MockReqType } from '@main/service/typing'
import { Response } from 'express'

@Controller('mock-api')
@ApiTags('mock 服务')
export class MockController {
  constructor(private readonly service: ApiService) {}

  @All(':projectFlag/**')
  async mockApi(@Param('projectFlag') projectFlag: string, @Req() request: Request, @Res() resp: Response) {
    const prefix = `/mock-api/${projectFlag}`
    let url = request.url.substring(prefix.length)
    let path = request['path'].substring(prefix.length)
    const mockReq: MockReqType = {
      method: request.method,
      fullUrl: request.url,
      url,
      path,
      headers: request.headers,
      body: request.body,
      query: request.query,
    }
    this.service.executeMockApi(projectFlag, mockReq, resp)
  }
}
